{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 标量乘法\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
   "metadata": {},
   "source": [
    "这段代码的主要目的是演示**标量乘法**（scalar multiplication）在向量上的作用。具体来说，它定义了一个二维向量，并将其与标量相乘，以观察向量的变化情况。\n",
    "\n",
    "---\n",
    "\n",
    "### **数学描述**\n",
    "\n",
    "1. **初始化向量**  \n",
    "   代码定义了一个二维向量 $a$：\n",
    "   $$\n",
    "   \\mathbf{a} = \\begin{bmatrix} 2 \\\\ 1 \\end{bmatrix}\n",
    "   $$\n",
    "   这个向量在二维平面上指向 $(2,1)$ 这个点。\n",
    "\n",
    "2. **标量乘法**  \n",
    "   代码定义了一个标量 $k = -2$，然后计算向量 $\\mathbf{a}$ 与标量 $k$ 的乘积：\n",
    "   $$\n",
    "   k \\mathbf{a} = (-2) \\cdot \\begin{bmatrix} 2 \\\\ 1 \\end{bmatrix}\n",
    "   = \\begin{bmatrix} -4 \\\\ -2 \\end{bmatrix}\n",
    "   $$\n",
    "   这个运算的几何意义是**将原向量沿其方向缩放，并翻转方向**：\n",
    "   - 由于 $|k| = 2$，新向量的长度是原向量的 $2$ 倍，即：\n",
    "     $$\n",
    "     \\| k\\mathbf{a} \\| = |k| \\cdot \\|\\mathbf{a}\\| = 2 \\|\\mathbf{a}\\|\n",
    "     $$\n",
    "   - 由于 $k$ 是负数，新向量的方向与 $\\mathbf{a}$ **相反**。\n",
    "\n",
    "3. **结论**  \n",
    "   计算结果是向量：\n",
    "   $$\n",
    "   \\mathbf{a'} = \\begin{bmatrix} -4 \\\\ -2 \\end{bmatrix}\n",
    "   $$\n",
    "   这个向量与原向量 $\\mathbf{a}$ **共线**（collinear），但方向相反，且长度是原向量的两倍。\n",
    "\n",
    "---\n",
    "\n",
    "### **总结**\n",
    "- 标量乘法会**改变向量的长度**（当 $|k| \\neq 1$ 时）。\n",
    "- 若 $k > 0$，向量方向不变；若 $k < 0$，向量方向翻转。\n",
    "- 计算后的向量始终与原向量**共线**。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27f1d742-511b-4ddb-beca-ff9ced2a9a0e",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6abc03f3-e5ad-44cc-a780-3e91b400e6c6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad5dd6c9-99ef-4c96-afdd-91d97e5a6243",
   "metadata": {},
   "source": [
    "## 定义向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ebb78725-794d-408e-b63d-ca88e55ffb45",
   "metadata": {},
   "outputs": [],
   "source": [
    "a_vec = np.array([3, 4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "adbe91b7-d5ec-492e-89de-2bbf2b5ccfda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.0"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.norm(a_vec)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1431be92-8111-499f-9c85-2caf2dc1a351",
   "metadata": {},
   "source": [
    "## 标量乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ff697021-6f3b-42f2-be08-6d5ac68b8ac5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-6, -8])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "k = -2\n",
    "k_a_vec = k * a_vec\n",
    "k_a_vec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "954b7525-dcef-47cb-af13-40c741ca3891",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10.0"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.linalg.norm(k_a_vec)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58ded0f4-b0b4-41ad-8a8a-521956853916",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
